package com.xiaozeng.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozeng.clients.ProductClient;
import com.xiaozeng.collect.mapper.CollectMapper;
import com.xiaozeng.collect.service.CollectService;
import com.xiaozeng.param.CollectParam;
import com.xiaozeng.param.ProductCollectParam;
import com.xiaozeng.pojo.Collect;
import com.xiaozeng.pojo.Product;
import com.xiaozeng.pojo.User;
import com.xiaozeng.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description:
 **/
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ProductClient productClient;
    @Override
    public Object save(CollectParam collectParam) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //分解参数
        Integer userId = user.getUserId();
        Integer productId = collectParam.getProductId();
        //数据库查询
        QueryWrapper<Collect> queryWrapper
                = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("product_id",productId);
        Long count = collectMapper.selectCount(queryWrapper);
        //判断是否存在收藏
        if ( count> 0){
            log.info("CollectServiceImpl.save业务结束，结果:{}",count);
            return Result.fail("商品已在收藏夹! 无需二次添加!");
        }
        //不存在，添加，并且提示添加成功即可
        //实体类封装
        Collect collect = new Collect();
        collect.setProductId(productId);
        collect.setUserId(userId);
        collect.setCollectTime(System.currentTimeMillis());
        //数据库插入
        collectMapper.insert(collect);
        //结果封装
        return Result.ok("商品添加成功!");
    }

    @Override
    public Result list() {
        //1.获取当前访问对象
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        //2.根据用户id查询商品id
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<Collect> list = collectMapper.selectList(queryWrapper);

        //3.结果封装
        List<Integer> ids = new ArrayList<>();
        for (Collect id : list) {
            ids.add(id.getProductId());
        }
        //判断是否为空
        if (ids.size() == 0){
            log.info("CollectServiceImpl.list业务结束，结果:{}","成功");
            return Result.ok(ids);
        }

        //调用商品服务
        ProductCollectParam productCollectParam = new ProductCollectParam();
        productCollectParam.setProductIds(ids);
        List<Product> productList = productClient.ids(productCollectParam);

        //结果封装
        return Result.ok(productList);
    }


    @Override
    public Object remove(CollectParam collectParam) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //参数封装
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.eq("product_id",collectParam.getProductId());

        int rows = collectMapper.delete(queryWrapper);

        log.info("CollectServiceImpl.remove业务结束，结果:{}",rows);

        return Result.ok("收藏移除成功!");
    }
}
